<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="form"
      element-loading-text="Loading"
      :header-cell-style="{ color: '#3F4756' }"
      stripe
      border
      fit
      highlight-current-row
      @row-click="handleEdit"
    >
      <el-table-column align="center" width="100" label="id" prop="id">
        <template />
      </el-table-column>
      <el-table-column align="center" width="100" label="年龄" prop="age">
        <template/>
      </el-table-column>
      <el-table-column align="center" label="性别" prop="sex">
        <template/>
      </el-table-column>
      <el-table-column align="center" label="工作" prop="job">
        <template/>
      </el-table-column>
      <el-table-column align="center" label="客户稳定程度(-1下降,0稳定,1上升)" prop="label">
        <template/>
      </el-table-column>
      <el-table-column align="center" label="修改设置">
        <el-button icon="el-icon-delete" type="danger" size="mini" @click="change">移除目标客户群</el-button>
      </el-table-column>
    </el-table>
    <br>
    <el-row>
      <el-col style="text-align:center">
        <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :page-sizes="[5, 10, 50, 100]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>

      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getList } from '@/api/table'
import axios from 'axios'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      id: 0,
      form: [], // 后端获取的数据
      show: [], // 当前显示的数据
      listLoading: true,
      total: 0, // 总数据条数
      currentPage: 1, // 当前所在页默认是第一页
      pageSize: 10 // 每页显示多少行数据 默认设置为10
    }
  },
  mounted() {
    this.getTotal()
    this.getData()
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleEdit(row) {
      this.id = row.id
      this.change()
    },
    change() {
      axios.get('http://127.0.0.1:8780/info-consumer/cancelGoal/' + this.id).then((response) => {
      })
    },
    getTotal() {
      axios.get('http://127.0.0.1:8780/info-consumer/getSelectGoal/').then((response) => {
        this.total = response.data
      })
    },
    getData() {
      axios.get('http://127.0.0.1:8780/info-consumer/selectGoal/' + this.currentPage + '/' + this.pageSize).then((response) => {
        this.form = response.data
      })
    },
    // 获取当前页的数据信息
    getPageInfo() {
      // 清空pageGrade中的数据
      this.show = []
      this.getData()
      // 获取当前页的数据
      for (let i = (this.currentPage - 1) * this.pageSize; i < this.total; i++) {
        // 把遍历的数据添加到pageGrade里面
        this.show.push(this.form[i])
        // 判断是否达到一页的要求
        if (this.show.length === this.pageSize) break
      }
    },
    // 分页时修改每页的行数,这里会自动传入一个size
    handleSizeChange(size) {
      // 修改当前每页的数据行数
      this.pageSize = size
      // 数据重新分页
      this.getPageInfo()
    },
    // 调整当前的页码
    handleCurrentChange(pageNumber) {
      // 修改当前的页码
      this.currentPage = pageNumber
      console.log(this.currentPage)
      // 数据重新分页
      this.getPageInfo()
    },
    fetchData() {
      this.listLoading = true
      getList().then(response => {
        this.list = response.data.items
        this.listLoading = false
      })
    }
  }
}
</script>
